iT邦幫忙

2022 iThome 鐵人賽

DAY 5
0
自我挑戰組

30天Java由淺入深系列 第 5

Day 5 : Variables( 1 )

  • 分享至 

  • xImage
  •  

#Java Variable

In programming languages, we call the value of the data stored “Variable”. There are mainly the following types:

  • String → stores a piece of text, the content must be between double quotes “ ”
  • int → stores an integer without decimal point
  • float → stores a number with decimal point (the declared number must be followed by the letter f)
  • char → stores a single character or ASCII value, e.g. j, C (contents must be enclosed in single quotes ' ')
  • boolean → stores the two states of a piece of data: true or false

The way to declare a variable in a program:

type variableName = value;
/* Example:
	  int number = 1206;
	  char grade = 'A';
	  String name = "Chi";    */

The usage modes and restrictions of each variable will be introduced one by one below!!!
(Markdown is included in a class, and only part of the code is displayed for instructional purposes.)


#Num

Integer

  1. byte (1 byte)
    Number range: -128 to 127

  2. short (2 bytes)
    Number range: -32768 to 32767

  3. int (4 bytes)
    Number range: 2147483648 to 2147483647

  4. long (8 bytes)
    Number range: 9223372036854775808 ~ 9223372036854775807 (declared value must be followed by the L identifier)

byte number1 = 126;
short number2 = 12060;
int number3 = 120600;
long number4 = 12060000000L

Floating Point

  1. float (4 bytes)
    Range of decimal places stored: approximately 6 to 7 decimal places

  2. double (8 bytes)
    Range of decimal places stored: approximately 15 decimal places

The respective identifier, f or d, must be added after the declared value.

float flnum = 12.6f;
double dounum = 12.666d;

Character

Occupied 2 bytes (bits) and can be used to store not only a single character, but also the values in the ASCII table.

char grade = 'S';
char first = 67, second = 72, third = 73;     /*也可以一次宣告多個字串*/
System.out.println(grade);           /* Outputs : S   */
System.out.print(first);             
System.out.print(second);                      
System.out.print(third);             /* Outputs : CHI */      

String

String is considered a relatively special Non-Primitive Data Type, and this section will introduce it in detail on Day 6.
Declare the use of the keyword String, and enclose the content in double quotation marks “”.

String name = "Chi";
String greeting = "Hello";
System.out.println(name + " " + greeting);  /* Outputs : Chi Hello */
																				    /*可以用 + 號把字串給串起來並印出*/

Boolean

Boolean type variables are declared using the keyword boolean, and their contents can only be accessed as true or false (lowercase)

boolean myNameIsChi = true;
boolean isYourNameYu = false;
System.out.println(myNameIsChi);        //Outputs : true
System.out.println(isYourNameYu);       //Outputs : false
  • Supplementary concept:
    We will learn about the use of loops in the future, and whether to execute the loop is determined by a conditional judgment.
    Boolean is also very useful here, because true and false can also be considered 1&0 (1->true, 0->false).
    Declaring a boolean value allows the loop to continue executing!!! (It's okay if you don't understand it here, I'll introduce it later.)
int num = 0;
boolean infinity = true;
while(infinity){           /*infinity內容為true,所以迴圈會一直執行進入*/
 if(num == 10){            /*直到num = 10時才停止跳出*/
		break;
	}
	num++;
}

上一篇
Day 4 : Java Basic Syntax
下一篇
Day 6 : Variables( 2 )
系列文
30天Java由淺入深30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言